package com.itheima.app.controller;

import com.itheima.app.manager.TestSoulManager;
import com.itheima.domain.mongo.TestAnswer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestSoulController {

    @Autowired
    private TestSoulManager testSoulManager;

    @GetMapping("/testSoul")
    public ResponseEntity findTestSoul(){
        return testSoulManager.findTestSoul();
    }

    @PostMapping("/testSoul")
    public ResponseEntity commitTest(@RequestBody Map<String,Object> answers){
        List<LinkedHashMap> testAnswers = (List<LinkedHashMap>) answers.get("answers");
        List<TestAnswer> testAnswerList = new ArrayList<>();
        for (LinkedHashMap<String,String> map : testAnswers) {
            TestAnswer testAnswer = new TestAnswer();
            testAnswer.setQuestionId(map.get("questionId"));
            testAnswer.setOptionId(map.get("optionId"));
            testAnswerList.add(testAnswer);
        }

        return testSoulManager.commitTest(testAnswerList);
    }

    @GetMapping("/testSoul/report/{id}")
    public ResponseEntity getReport(@PathVariable String id){
        return testSoulManager.getReport(id);
    }

}
